package pattern.Composite;

import java.util.ArrayList;

/**
 * Client
 * Create on 2017/5/10
 * <p>
 * <p>
 * 更多的相关信息请访问
 * 组合模式（Composite）的安全模式与透明模式http://haolloyin.blog.51cto.com/1177454/347308
 * 职责链模式（Chain of Responsibility）的Java实现http://haolloyin.blog.51cto.com/1177454/342166
 *
 * @author Asin Liu
 * @version 1.0
 */
public class Client {

    public static void main(String[] args) {

        // 创建过滤器树: 包含"a"、"b"、"cd"子串,以"end"结尾,长度 > 7

        // 同时包含"a"、"b"、"cd"子串的过滤器
        CompositeFilter andFilter = new AndFilter();

        IFilter contain01 = new ContainsFilter("a");
        IFilter contain02 = new ContainsFilter("b");
        IFilter contain03 = new ContainsFilter("cd");
        andFilter.addFilter(contain01);
        andFilter.addFilter(contain02);
        andFilter.addFilter(contain03);

        // 以 "end" 或 "END" 结尾的过滤器
        CompositeFilter orFilter = new OrFilter();

        IFilter end01 = new EndFilter("end");
        IFilter end02 = new EndFilter("END");
        orFilter.addFilter(end01);
        orFilter.addFilter(end02);

        // 长度 > 7 的过滤器
        IFilter lengthFilter = new LengthFilter(7);

        // 构建过滤器树，用根过滤器将上面的所有过滤器组合起来
        CompositeFilter rootFilter = new AndFilter();

        rootFilter.addFilter(andFilter);
        rootFilter.addFilter(orFilter);
        rootFilter.addFilter(lengthFilter);

        // 自定义一本字典里的所有单词
        ArrayList<String> allWords = new ArrayList<>();
        allWords.add("akkkk");
        allWords.add("ab--b-cd--end");
        allWords.add("abckk");
        allWords.add("abdcend");// 长度为6,不符合
        allWords.add("kakbkck");
        allWords.add("a-b-cd-END");
        allWords.add("bbcc");

        // 自定义一本字典
        Dictionary dictionary = new Dictionary(allWords);

        // 将字典、过滤器树传入管道
        Pipe pipe = new Pipe(dictionary, rootFilter);

        // 单词流在通过管道时被过滤，获取最终单词
        System.out.println(pipe.getWords());
    }
}